<?php
declare (strict_types = 1);

namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
class Cate extends BaseController
{
    public function cate()
    {

        $id=Request::get('id',0,'trim,htmlspecialchars,strip_tags');
        $data = Request::get('','','trim,htmlspecialchars,strip_tags');

        $list=\app\index\model\Gcategory::getcate($data,$data,[],$id);




        $empty="<tr><td align='conter' colspan='7'>没有数据</td></rt>";
        View::assign('empty',$empty);
        return View::fetch('',['list'=>$list]);
    }
    public function cate_add(){

        $parent_id = Request::get('parent_id','','trim,htmlspecialchars,strip_tags');
        View::assign('parent_id',$parent_id);
        $cate=\app\index\model\Gcategory::getGcategory();
        View::assign('cate',$cate);
        if(Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');

            try {

                validate(\app\validate\Cate::class)->check($post);
            } catch (\think\exception\ValidateException $e) {
                // 验证失败 输出错误信息
                $this->error($e->getMessage());
            }

            $jumpPath='';
            if('1'==$post['button'])   $jumpPath = url('cate');
            if ('2'==$post['button'])  $jumpPath = url('cate_add');

            $cateObj = new \app\index\model\Gcategory();
            $result = $cateObj->add($cateObj,$post);


            if($result){
                $this->success("成功！".$cateObj->getInsertId(),$jumpPath) ;

            }else{
                $this->error( "失败！原因：".$cateObj->getError());
            }
        }else{
            return View::fetch();
        }
    }

    //修改

    public function cate_edit(){
        $param=Request::param('param',[],'trim,htmlspecialchars,strip_tags');
        $cate_id = Request::get('cate_id','','trim,htmlspecialchars,strip_tags');
        View::assign('cate_id',$cate_id);
        $cate=\app\index\model\Gcategory::getGcategory();
        View::assign('cate',$cate);

        if(Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');

            try {

                validate(\app\validate\Cate::class)->scene('edit')->check($post);
            } catch (\think\exception\ValidateException $e) {
                // 验证失败 输出错误信息
                $this->error($e->getMessage());
            }

            $cateObj = new \app\index\model\Gcategory();
            $result = $cateObj->edit($post);

            if($result){
                $this->success('修改成功！',url('cate',$param));
            }else{
                $this->error('修改失败！');
            }

        }else{
            // 显示修改的页面
            $id = Request::get('id','','trim,htmlspecialchars,strip_tags');

            $cateObj = \app\index\model\Gcategory::find($id);
            if(empty($cateObj)){
                $this->error('参数错误！',url('cate'));
            }

            return View::fetch('',$cateObj->toArray());
        }

    }

    //删除
    public function del(){
        $id =Request::get('id','','');
        //先查询分类表和其他表有没有关联
        $goods= Db::table('ecm_goods')->where('cate_id',$id)->count();
        //然后在查询下级分类有没有
        $cate= Db::table('ecm_gcategory')->where('parent_id',$id)->count();
	//如果上两条都没有，就可以进行删除
        Db::startTrans();
        if ($goods==0 && $cate==0){
            try{
                \app\index\model\Gcategory::destroy($id,true);

                //提交事务
                Db::commit();

            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('删除成功!',getenv("HTTP_REFERER"));

        }else{
            // 回滚事务
            Db::rollback();
            $this->error('删除失败，分类还有下一级或商品已被占用！');
            return View::fetch();
        }
    }
}
